import csv
import re


def parse_line(line):
    # 分割行，maxsplit=1 确保只分割一次
    parts = line.strip().split(' ', 1)

    # 如果分割后的部分只有1个，那么说明没有英文部分
    if len(parts) == 1:
        chinese = parts[0]
        abbreviation, english = "", ""
    else:
        # 第一部分可能包含缩写和中文
        potential_abbr_and_chinese, english = parts

        # 查找连续的大写字母缩写
        match = re.search(r'^([A-Z0-9]+)', potential_abbr_and_chinese)
        if match:
            # 如果找到了缩写，将缩写和剩下的中文部分分开
            abbreviation = match.group(1)
            chinese = potential_abbr_and_chinese[len(abbreviation):].strip()
        else:
            # 如果没有找到缩写，那么整个第一部分都是中文
            abbreviation, chinese = "", potential_abbr_and_chinese

    return abbreviation, chinese, english


def convert_txt_to_csv(txt_file, csv_file):
    with open(txt_file, 'r', encoding='utf-8') as file:
        lines = file.readlines()

    with open(csv_file, 'w', newline='', encoding='utf-8') as csvfile:
        writer = csv.writer(csvfile)
        writer.writerow(['缩写', '中文', '英文'])  # 写入标题行

        for line in lines:
            abbreviation, chinese, english = parse_line(line)
            writer.writerow([abbreviation, chinese, english])


# 使用方法
txt_file_path = 'input.txt'
csv_file_path = 'output_txqw.csv'
convert_txt_to_csv(txt_file_path, csv_file_path)